package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.Login;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;



    /**
     * 修改用户信息
     * @param userInfoVo
     * @return
     * //http://localhost/api/user/wxLogin/updateUser
     */
    @Operation(summary = "修改用户的信息")
    @PostMapping("/updateUser")
    @Login
    public Result updateUser(@RequestBody UserInfoVo userInfoVo){
        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        //调用服务层的方法
        userInfoService.updateUser(userId,userInfoVo);
        //返回结果
        return Result.ok();
    }

    /**
     * 获取用户信息
     * @return
     * //http://localhost/api/user/wxLogin/getUserInfo
     */
    @Operation(summary = "获取用户信息")
    @GetMapping("/getUserInfo")
    @Login
    public Result getUserInfo(){
        //获取用户的id
        Long userId = AuthContextHolder.getUserId();
        //调用服务层方法
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        return Result.ok(userInfoVo);
    }


    /**
     * 微信登录
     * //http://localhost/api/user/wxLogin/wxLogin/0b3UVNkl2AOydg4Owrol2naql54UVNkk
     * @param code
     * @return
     */
    @Operation(summary = "微信登录")
    @GetMapping("/wxLogin/{code}")
    public Result wxLogin(@PathVariable String code){
        //调用服务层方法
        HashMap<String,Object> resultMap = userInfoService.wxLogin(code);
        //返回结果
        return Result.ok(resultMap);
    }
}
